package cn.yunhe.action;

import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.service.INewsService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

public class NewsAction extends ActionSupport implements ModelDriven {
     @Resource
    private INewsService newsService;
    private News news;
    private  int pageSize;
    private int pageNum;
    private Page page;
    public  String queryLikeNews(){
        Map<String,Object> cond = new HashMap<String, Object>();
        cond.put("pageSize",pageSize);
        cond.put("pageNum",pageNum);
        cond.put("news",news);
        page = newsService.queryLikeNews(cond);
        return  SUCCESS;
    }
    public  String addNews(){
        newsService.addNews(news);
        return SUCCESS;
    }

    public  String delNews(){
        newsService.delNews(news);
        return SUCCESS;
    }
    public  String getNewsById(){
        News n = newsService.getNewsById(news);
        Map request =(Map) ActionContext.getContext().get("request");
        request.put("news",n);
        return  SUCCESS;
    }
    public  String updateNews(){
        newsService.updateNews(news);
        return  SUCCESS;
    }

    public INewsService getNewsService() {
        return newsService;
    }

    public void setNewsService(INewsService newsService) {
        this.newsService = newsService;
    }

    public News getNews() {
        return news;
    }

    public void setNews(News news) {
        this.news = news;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    @Override
    public Object getModel() {
        if(news == null){
            news = new News();
        }
        return news;
    }
}
